package com.hugesoft.logic.managers.driverlicencemanager;

import java.util.List;

import android.os.Handler;

import com.hugesoft.commonlogic.dao.DbManager;
import com.hugesoft.logic.managers.base.BaseBusinessLogicManager;
import com.hugesoft.logic.managers.common.Constans;
import com.hugesoft.logic.module.Bus;
import com.hugesoft.logic.module.BusDao;
import com.hugesoft.logic.module.DriverLicence;
import com.hugesoft.logic.module.DriverLicenceDao;

import de.greenrobot.dao.Property;
import de.greenrobot.dao.query.QueryBuilder;

public class DriverLicenceManager extends BaseBusinessLogicManager implements IDriverLicenceManagerInterface{

	@Override
	public void addlicenceinfo(String licenceno, String type,
			String drivername, int driverage, Handler handler) {
		DriverLicence licence = new DriverLicence(null, licenceno, type, drivername, driverage);
		try {
			DbManager.getDriverlicenceDao().insert(licence);
			handleaddlicenceresponse(true,handler,licence);
		} catch (Exception e) {
			handleaddlicenceresponse(false, handler,null);
		}
	}

	@Override
	public DriverLicence loadlicenceinfo(String licenceno) {
		List<DriverLicence> ret = null;
		QueryBuilder<DriverLicence> qBuilder = DbManager.getDriverlicenceDao()
				.queryBuilder();
		qBuilder.where(
				DriverLicenceDao.Properties.Licenceno.eq(licenceno));
		qBuilder.orderAsc(DriverLicenceDao.Properties.Licenceno);

		ret = qBuilder.list();
		if (ret == null || ret.size() == 0) 
		{
			return null;
		}
		else
		{
			return ret.get(0);
		}
	}

	private void handleaddlicenceresponse(boolean isSuccess,Handler handler,Object data)
	{
		handleresponse(isSuccess?Constans.ADD_LICENCE_SUCCESS:Constans.ADD_LICENCE_FAILED, data, handler);
	}

	public DriverLicence loadlicenceinfo(Long licenceId)
	{
		List<DriverLicence> ret = null;
		QueryBuilder<DriverLicence> qBuilder = DbManager.getDriverlicenceDao()
				.queryBuilder();
		qBuilder.where(
				DriverLicenceDao.Properties.Id.eq(licenceId));
		qBuilder.orderAsc(DriverLicenceDao.Properties.Id);

		ret = qBuilder.list();
		if (ret == null || ret.size() == 0) 
		{
			return null;
		}
		else
		{
			return ret.get(0);
		}
	}

	@Override
	public List<DriverLicence> loadalldriverlicence() {
		List<DriverLicence> ret = null;
		QueryBuilder<DriverLicence> qBuilder = DbManager.getDriverlicenceDao()
				.queryBuilder();
		qBuilder.orderAsc(DriverLicenceDao.Properties.Id);
		ret = qBuilder.list();
		return ret;
	}
}
